const app = getApp()
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk;

Page({
  data: {
    serverHost : app.config.serverHost,
    address:"",
    show: false,
    screenWidth: 0,
    imgwidth: 0,
    imgheight: 0,
    ifs:false,
    beijing:"https://www.nj246.com/images/背景图.png",
    imgUrls: [
      "https://www.nj246.com/images/图标3.png",
      "https://www.nj246.com/images/图标2.png",
      "https://www.nj246.com/images/图标1.png",
      "https://www.nj246.com/images/图标4.png"
    ],
    duration: 800,
    swiperCurrent: 0,
    iconList: [{
      imgUrl: "/images/新鲜蔬果.png",
      text: "时令水果"
    }, {
        imgUrl: "/images/新鲜蔬菜.png",
      text: "新鲜蔬菜"
    }, {
        imgUrl: "/images/肉禽蛋类.png",
      text: "肉禽蛋类"
    }, {
        imgUrl: "/images/海鲜水产.png",
      text: "海鲜水产"
    }, {
        imgUrl: "/images/酒水饮料.png",
      text: "酒水饮料"
    }, {
        imgUrl: "/images/零食熟食.png",
      text: "零食熟食"
    }, {
        imgUrl: "/images/粮油速食.png",
      text: "粮油速食"
    }, {
        imgUrl: "/images/纸品清洁.png",
      text: "纸品清洁"
    }],
    goodsList: ''
  },
  // 轮播图函数
  swiperChange(e) {
    let current = e.detail.current;
    this.setData({
      swiperCurrent: current,
    })
  },
  fenLei(e){
    app.globalData.index = e.currentTarget.dataset.index + 1
    app.globalData.text = e.currentTarget.dataset.text
    wx.switchTab({
      url: '/pages/sort/index'
    });
  },
  getLocalName: function () {
        const that = this
        qqmapsdk.reverseGeocoder({
            poi_options: 'policy=2',
            success: function (res) {
              if (wx.getStorageSync('service')){
                let district = wx.getStorageSync('service')
                console.log(district)
                app.api.getData('web/localgoodslist', { district: district }, (res) => {
                  app.globalData.goodslist = res.data
                  let temp = res.data.filter(item => {
                    return item.status == '热门'
                  })
                  that.setData({
                    goodsList: temp
                  })
                })
              }else{
                let district = res.result.address_component.district
                app.api.getData('web/goodslist', { district: district }, (res) => {
                  app.globalData.goodslist = res.data
                  let temp = res.data.filter(item => {
                    return item.status == '热门'
                  })
                  that.setData({
                    goodsList: temp
                  })
                })
              }

            
              app.globalData.address = res.result.address_component.district
              if(wx.getStorageSync('service') == ''){
                that.setData({
                  address: res.result.address
                })
              }else{
                that.setData({
                  address:wx.getStorageSync('service')
                })
              } 
            }
        });
    },
  clickMap(){
    wx.navigateTo({
      url: '../map/index'
    })
  },
  onLoad(){
    let _this = this

    
    qqmapsdk = new QQMapWX({
      key: 'J5NBZ-RA7KP-NYNDD-VNBCU-ICXHO-R6F3P'
    });
    _this.getLocalName();
    // if(wx.getStorageSync('service') == ''){
    //   _this.getLocalName()
    // }else{
    //   _this.setData({
    //     address:wx.getStorageSync('service')
    //   })
    // }
    if(!wx.getStorageSync('userInfo')){
      _this.setData({
        show: true
      })
    }
    if (app.globalData.userInfo) {
      _this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    }else if(_this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        _this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    }else{
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          _this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          screenWidth: res.windowWidth,
        })
      }
    })
    app.globalData.text = '全部'
    this.imageLoad()
  },
  onShow(){
    if(wx.getStorageSync('service') != ''){
      this.setData({
        ifs:true,
        address:wx.getStorageSync('service')
      })
    }
  },
  imageLoad: function (e) {
    var screenW = this.data.screenWidth;
    var viewWidth = 320 * screenW / 750;
    var viewHeight = viewWidth;
    this.setData({
      imgwidth: viewWidth,
      imgheight: viewHeight
    })
  },
  getUserInfo(userInfo) {
    if (!userInfo.detail.rawData || !userInfo.detail.signature) {
      wx.showModal({
        title: '提示',
        content: '你没有授权,不能享受菜多多的贴♥服务哦'
      })
      return
    }
    wx.setStorageSync('userInfo', userInfo.detail.rawData)
    this.setData({
      show: false
    })
  },
  // 跳转广告函数
  goAd:function(){
    wx.navigateTo({
      url: '/pages/index/ad/index'
    });
  },
  // 跳转到商品详情页面
  goXiangQing:function(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/goodsDetail/goods?id=' + id
    });
  },
  goXiangQings:function(e){
    wx.showToast({
      title: '商品已售空',
      image:'../../static/icons/err.png',
      duration: 1000,
      mask: true    //是否有透明蒙层，默认为false 
      //如果有透明蒙层，弹窗的期间不能点击文档内容 
    })
  }
})